//
//  LSTPopViewrRSidebarView.m
//  LSTPopView_Example
//
//  Created by LoSenTrad on 2020/4/24.
//  Copyright © 2020 490790096@qq.com. All rights reserved.
//

#import "LWFuncView.h"
#import "UIColor+LSTColor.h"
#import "UIView+LSTView.h"
#import "CCDTableViewCell.h"
#import "ZGDeviceInfo.h"
#import "LWSwithCell.h"
#import "UIColor+HEX.h"
#import "Masonry.h"
#import "CDModelClick.h"

@interface LWFuncView () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic, strong) NSMutableDictionary *cellDic;

/** <#.....#> */
@property (nonatomic,strong) UIButton *btn1;
/** <#.....#> */
@property (nonatomic,strong) UIButton *btn2;
/** <#.....#> */
@property (nonatomic,strong) UIButton *btn3;
/** <#.....#> */
@property (nonatomic,strong) UIButton *btn4;
/** <#.....#> */
@property (nonatomic,strong) UIButton *btn5;
/** <#.....#> */
@property (nonatomic,strong) UIButton *btn6;


/* 视图 */
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,  strong) UIVisualEffectView *blurView;



@end

@implementation LWFuncView

- (NSMutableDictionary *)cellDic {
    
    if (_cellDic == nil) {
        
        _cellDic = [NSMutableDictionary dictionary];
    }
    
    return _cellDic;;
}

#pragma mark - ***** 初始化 *****

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.layer.cornerRadius = 7;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor colorWithHex:0x000000 andAlpha:1];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(photoPreviewCollectionViewDidScroll:) name:@"InstallRefreshData" object:nil];
        [self initSubViews:frame];
    }
    return self;
}



#pragma mark - ***** setter 设置器/数据处理 *****

- (UITableView *)tableView {
    
    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, self.bounds.size.width, self.bounds.size.height) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.estimatedRowHeight = 44;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        
        _tableView.rowHeight = UITableViewCellSeparatorStyleNone;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        //这行代码必须加上，可以去除tableView的多余的线，否则会影响美观
        //        _tableView.tableFooterView = [UIView new];
        ///设置table没有分割线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        
        [_tableView registerClass:[LWSwithCell class] forCellReuseIdentifier:@"LWSwithCell"];
        
    }
    
    return _tableView;
}

#pragma mark - ***** setupUI 界面布局 *****

- (void)initSubViews:(CGRect)frame {
    
    self.blurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    
    self.blurView.frame = frame;
    [self addSubview:self.blurView];
    
    [self.blurView.contentView addSubview:self.tableView];
    
    self.headerView = [UIView new];
    
    self.headerView.frame = CGRectMake(0, 0, frame.size.width, 125);
    [self addSubview:self.headerView];
    self.lable5 = [UILabel new];
    self.lable5.textColor = [UIColor redColor];
    self.lable5.font = [UIFont boldSystemFontOfSize:17];
    self.lable5.textAlignment = 1;
    self.lable5.text = @"小K助手";
    [self.headerView addSubview:self.lable5];
    
    [self.lable5 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.offset(0);
        make.top.offset(5);
        
    }];
    
    self.startLab = [UILabel new];
    self.startLab.textColor = [UIColor whiteColor];
    self.startLab.font = [UIFont boldSystemFontOfSize:14];
    self.startLab.textAlignment = 1;
    self.startLab.text = @"上限:";
    [self.headerView addSubview:self.startLab];
    
    [self.startLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(3);
        make.top.equalTo(self.lable5.mas_bottom).offset(12);
        
    }];
    
    self.startTextFiled = [[SCLTextView alloc] init];
    self.startTextFiled.placeholder = @"请输入上限";
    self.startTextFiled.delegate = self;
    self.startTextFiled.text = [CDModelClick sharepayManger].endFan;
    
    self.startTextFiled.layer.cornerRadius = 4;
    self.startTextFiled.layer.masksToBounds = YES;
    self.startTextFiled.font = [UIFont systemFontOfSize:14];
    self.startTextFiled.layer.borderWidth = 1;
    self.startTextFiled.layer.borderColor = [UIColor whiteColor].CGColor;
    [self.startTextFiled addTarget:self action:@selector(startTextFiledChange:) forControlEvents:UIControlEventEditingChanged];
    [self.blurView.contentView addSubview:self.startTextFiled];
    
    [self.startTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.startLab.mas_right).offset(2);
        make.width.offset(142);
        make.top.equalTo(self.lable5.mas_bottom).offset(10);
        make.height.offset(25);
        
    }];
    
    self.endLab = [UILabel new];
    self.endLab.textColor = [UIColor whiteColor];
    self.endLab.font = [UIFont boldSystemFontOfSize:14];
    self.endLab.textAlignment = 1;
    self.endLab.text = @"下限:";
    [self.headerView addSubview:self.endLab];
    
    [self.endLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(3);
        make.top.equalTo(self.startLab.mas_bottom).offset(15);
        
    }];
    
    self.endTextFiled = [[SCLTextView alloc] init];
    self.endTextFiled.placeholder = @"请输入下限";
//    self.endTextFiled.text = @"0x100000000";
    self.endTextFiled.text = [CDModelClick sharepayManger].startFan;
    self.endTextFiled.delegate = self;
    self.endTextFiled.layer.cornerRadius = 4;
    self.endTextFiled.layer.masksToBounds = YES;
    self.endTextFiled.font = [UIFont systemFontOfSize:14];
    self.endTextFiled.layer.borderWidth = 1;
    self.endTextFiled.layer.borderColor = [UIColor whiteColor].CGColor;
    [self.endTextFiled addTarget:self action:@selector(endTextFiledChange:) forControlEvents:UIControlEventEditingChanged];
    [self.blurView.contentView addSubview:self.endTextFiled];
    
    [self.endTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.endLab.mas_right).offset(2);
        make.width.offset(142);
        make.top.equalTo(self.startLab.mas_bottom).offset(12);
        make.height.offset(25);
        
    }];
    
    
    
    self.nearbyLab = [UILabel new];
    self.nearbyLab.textColor = [UIColor whiteColor];
    self.nearbyLab.font = [UIFont boldSystemFontOfSize:14];
    self.nearbyLab.textAlignment = 1;
    self.nearbyLab.text = @"临近:";
    [self.headerView addSubview:self.nearbyLab];
    
    [self.nearbyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(3);
        make.top.equalTo(self.endLab.mas_bottom).offset(15);
        
    }];
    
    self.nearbyTextFiled = [[SCLTextView alloc] init];
    self.nearbyTextFiled.placeholder = @"请输入下限";
//    self.nearbyTextFiled.text = @"0x20";
    self.nearbyTextFiled.text = [CDModelClick sharepayManger].nearbyFan;
    self.nearbyTextFiled.layer.cornerRadius = 4;
    self.nearbyTextFiled.delegate = self;
    self.nearbyTextFiled.layer.masksToBounds = YES;
    self.nearbyTextFiled.font = [UIFont systemFontOfSize:14];
    self.nearbyTextFiled.layer.borderWidth = 1;
    self.nearbyTextFiled.layer.borderColor = [UIColor whiteColor].CGColor;
    [self.nearbyTextFiled addTarget:self action:@selector(nearbyTextFiledChange:) forControlEvents:UIControlEventEditingChanged];
    [self.blurView.contentView addSubview:self.nearbyTextFiled];
    
    [self.nearbyTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.nearbyLab.mas_right).offset(2);
        make.width.offset(142);
        make.top.equalTo(self.endLab.mas_bottom).offset(12);
        make.height.offset(25);
        
    }];
    
    
    
    
    
    //    self.lable4 = [UILabel new];
    //    self.lable4.textColor = [UIColor whiteColor];
    //    self.lable4.font = [UIFont boldSystemFontOfSize:13];
    //    self.lable4.textAlignment = 1;
    //    self.lable4.text = @"我是一个简介";
    //
    //    [self.headerView addSubview:self.lable4];
    //    [self.lable4 mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //        make.centerX.offset(0);
    //        make.top.offset(40);
    //
    //    }];
    //
    //
    //    self.lable3 = [UILabel new];
    //    self.lable3.textColor = [UIColor whiteColor];
    //    self.lable3.font = [UIFont boldSystemFontOfSize:13];
    //    self.lable3.textAlignment = 1;
    //    self.lable3.text = [NSString stringWithFormat:@"%@ iOS %@",[ZGDeviceInfo currentDeviceInfo].getCurrentDevicePhoneType,[ZGDeviceInfo currentDeviceInfo].getSystemVersion];
    //
    //    [self.headerView addSubview:self.lable3];
    //    [self.lable3 mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //        make.centerX.offset(0);
    //        make.top.offset(66);
    //
    //    }];
    //
    //    self.lable2 = [UILabel new];
    //    self.lable2.textColor = [UIColor whiteColor];
    //    self.lable2.font = [UIFont boldSystemFontOfSize:11];
    //    self.lable2.textAlignment = 1;
    //    self.lable2.text = [NSString stringWithFormat:@"当前时间: %@",[UKCommonTools getCurrentTimes]];
    //
    //    [self.headerView addSubview:self.lable2];
    //    [self.lable2 mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //        make.centerX.offset(0);
    //        make.top.offset(90);
    //
    //    }];
    
    
    
    
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    ////            self.tableView.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    //
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    //            self.btn2.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    //
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    //            self.btn3.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    //
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    //            self.btn4.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    //
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    //            self.btn5.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    //
    //
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    //            self.btn6.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    
    
    
}

#pragma mark tableDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.titleArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *identifier = [self.cellDic objectForKey:[NSString stringWithFormat:@"LWSwithCell%@",indexPath]];
    // 如果取出的唯一标示符不存在，则初始化唯一标示符，并将其存入字典中，对应唯一标示符注册Cell
    if (identifier == nil) {
        identifier = [NSString stringWithFormat:@"%@%@", @"LWSwithCell", [NSString stringWithFormat:@"%@", indexPath]];
        [self.cellDic setValue:identifier forKey:[NSString stringWithFormat:@"%@", indexPath]];
        // 注册Cell
        [self.tableView registerClass:[LWSwithCell class] forCellReuseIdentifier:identifier];
    }
    
    LWSwithCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    cell.titleLab.text = self.titleArr[indexPath.row];
    
    cell.switchBtn.tag = indexPath.row;
    
    [cell.switchBtn addTarget:self action:@selector(titleSwitchClck:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return 36;
}

- (void)setTitleArr:(NSArray *)titleArr {
    
    _titleArr = titleArr;
    
    [self.tableView reloadData];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    
    [[LWJRModelFunc sharepayManger] playFuncName:self.titleArr[indexPath.row] andState:YES];
    
}

#pragma mark - 开关

- (void)titleSwitchClck:(UISwitch *)sw {
    
  
    NSLog(@"当前tag=%ld",(long)sw.tag);
    
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    
}

#pragma mark - ***** other 其他 *****


#pragma mark -
- (void)photoPreviewCollectionViewDidScroll:(NSNotification *)not {
    
    NSString *title = not.object;
    
    self.lable5.text = title;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    if (textField == self.nearbyTextFiled) {
        
        [CDModelClick sharepayManger].nearbyFan = textField.text;
        
    } else if (textField == self.startTextFiled) {
        
        [CDModelClick sharepayManger].endFan = textField.text;
        
    } else if (textField == self.endTextFiled) {
        
        [CDModelClick sharepayManger].startFan = textField.text;
    }
    
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];

    return YES;
}

#pragma mark 上限监听
- (void)startTextFiledChange:(UITextField *)textField {
//    self.smsBtn.enabled = textField.text.length==11;

    [CDModelClick sharepayManger].endFan = textField.text;
}

#pragma mark 下限监听
- (void)endTextFiledChange:(UITextField *)textField {

//    self.smsBtn.enabled = textField.text.length==11;
    [CDModelClick sharepayManger].startFan = textField.text;
}

#pragma mark 临近监听
- (void)nearbyTextFiledChange:(UITextField *)textField {
//    self.smsBtn.enabled = textField.text.length==11;
    
    [CDModelClick sharepayManger].nearbyFan = textField.text;
}

@end
